package com.xls.boxlibrary.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;


/**
 * RecyclerView的item视图基类
 */

public abstract class MvpRVViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

    public T viewData;//数据源

    public View.OnClickListener onClickListener;//控件点击事件

    public View.OnLongClickListener onLongClickListener;//控件长按事件

    public Context context;

    public MvpRVViewHolder(int itemView, Context context) {
        super(View.inflate(context, itemView, null));
        this.context=context;
    }

    public MvpRVViewHolder(int itemView, Context context, ViewGroup p) {
        super(LayoutInflater.from(context).inflate(itemView, p, false));
        this.context=context;
    }

    /**
     * 设置数据  一定要在  initView之前
     *
     * @param data
     */
    public void setViewData(T data) {
        this.viewData = data;
    }

    /**
     * 初始化视图
     */
    public abstract void initView();

    /**
     * 视图点击事件
     *
     * @param onClickListener
     */
    public void setOnClickListener(View.OnClickListener onClickListener) {
        if (onClickListener != null) {
            this.onClickListener = onClickListener;
        }
    }

    /**
     * 视图长按事件
     *
     * @param onLongClickListener
     */
    public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
        if (null != onLongClickListener) {
            this.onLongClickListener = onLongClickListener;
        }
    }

    @Override
    public void onClick(View v) {
        if (onClickListener != null) {
            View view=new View(context);
            view.setId(v.getId());
            view.setTag(getAdapterPosition()+1);
            onClickListener.onClick(view);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (onLongClickListener != null) {
            View view=new View(context);
            view.setId(v.getId());
            view.setTag(getAdapterPosition());
            onLongClickListener.onLongClick(view);
            return true;
        }
        return false;
    }

    public T getViewData() {
        return viewData;
    }
}
